package cn.zzmx.service;

import cn.zzmx.entity.Product;
import cn.zzmx.mapper.IProductMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ,readOnly = false)
public class IProductServiceImpl implements IProductService{

    @Autowired
    private IProductMapper iProductMapper;

    @Override
    @Transactional(readOnly = true)
    public PageInfo<Product> queryProductAll(Integer page, Integer limit) {

        //分页处理  page当前页面，limit页面大小
        PageHelper.startPage(page,limit);
        PageInfo<Product> productPageInfo = new PageInfo<Product>(iProductMapper.queryProductAll());

        return productPageInfo;
    }

    @Override
    public void deleteProducts(String[] ids) {
        for (String id : ids) {
            iProductMapper.deleteProductById(id);
        }
    }

    @Override
    public void deleteProductById(String id) {
        iProductMapper.deleteProductById(id);
    }

    @Override
    public void addProduct(Product product) {
        iProductMapper.addProduct(product);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Product> queryProductById(String id) {

        List<Product> list = new ArrayList<Product>();
        list.add(iProductMapper.queryProductById(id));
        return list;
    }

    @Override
    public void updateProduct(Product product) {
        iProductMapper.updateProduct(product);
    }
}
